var num = 0;
var form;
jQuery(function () {
    layui.use(['layer', 'form'], function () {
        form = layui.form();
        init();
    });
});
function init() {
    getArea(1,1);
    $("#btn").click(next);

    doubleClick("img1",function(obj){
        $(obj).parent().hide();
        $(obj).attr("src","")
        $("#filePicker").show();
    });
    doubleClick("img2",function(obj){
        $(obj).parent().hide();
        $(obj).attr("src","")
        $("#filePicker").show();
    });

    doubleClick("img3",function(obj){
        $(obj).parent().hide();
        $(obj).attr("src","")
        $("#filePicker").show();
    });
}
var getArea = function(parent,servel){
    $.ajax({
        url:server_url+'rest/areaManager/getByParent.do?parentId='+parent,
        data:{ran:Math.random()},
        type:'get',
        error:function(){alert(contants.systemError);},
        success:function(msg){
            //console.log(msg);
            var areas = msg.data;
            var s = '';
            for(var i=0;i<areas.length;i++){
                s +='<option value="'+areas[i].areaId+','+areas[i].areaName+'">'+areas[i].areaName+'</option>';
            }
            if(servel ==1){
                s= '<option value="">请选择省</option>' +s;
                $("#consProvince1").html(s);
                form.render("select");
                form.on('select(consProvince)', function(data){
                    var pro = data.value.substring(0,data.value.indexOf(","));
                    $("#consProvince").val(data.value.substring(data.value.indexOf(",")+1));
                    getArea(pro,2);
                });

            }else if(servel == 2){
                s= '<option value="">请选择市</option>' +s;
                $("#consCity1").html(s);
                form.render("select");
                form.on('select(consCity)', function(data){
                    $("#consCity").val(data.value.substring(data.value.indexOf(",")+1));
                    getArea(data.value.substring(0,data.value.indexOf(",")),3)
                });
            }else if(servel == 3){
                s= '<option value="">请选择县</option>' +s;
                $("#consCountry1").html(s);
                form.render("select");
                form.on('select(consCountry)', function(data){
                    var pro = data.value;
                    $("#consCountry").val(data.value.substring(data.value.indexOf(",")+1));
                });
            }

        }

    })
}
var b = false;
function next() {

    if($("#price").val() == "")
    {
        easyAlert("请输入报价");
        return false;
    }
    if (!isPriceNumber($("#price").val())) {
        easyAlert("请输入正确的价格格式！");
        return false;
    }
    if($("#amount").val() == "")
    {
        easyAlert("请输入可供数量，只接受数字");
        return false;
    }
    if (!isInt($("#amount").val())) {
        easyAlert("输入正确的可供数量,只接受数字！");
        return false;
    }
    if ($("#consProvince").val() == null || $("#consProvince").val()== '') {
        easyAlert("请选择收货省！");
        return false;
    }
    if ($("#consCity").val() == null || $("#consCity").val()== '') {
        easyAlert("请选择收货市！");
        return false;
    }
    if ($("#consCountry").val() == null || $("#consCountry").val()== '') {
        easyAlert("请选择收货县！");
        return false;
    }
    var deliveryArea = $("#consProvince").val()+$("#consCity").val()+$("#consCountry").val();
    var img1 = $("#img1").attr("src");
    var img2 = $("#img2").attr("src");
    var img3 = $("#img3").attr("src");
    if(img1 == '' && img2 == '' && img3 == '')
    {
        easyAlert("请至少上传一张图片");
        return ;
    }
    if(b){
        return;
    }
    b = true;
    $.ajax({
        url: server_url + '/rest/buyer/demand/demandSupply.do?userId='+userid+"&userid="+userid+"&area="+deliveryArea
            +"&demandid="+params()['id'] + "&imgurl1="+img1+"&imgurl2="+img2+"&imgurl3="+img3+"&ran="+Math.random(),
        data: $("form").serialize(),
        type:"post",
        success: function (data) {
            easyAlert("报价成功！");
            window.location.href = server_url + '/buyer/demand/source.do';
        },
        error:function(){
            easyAlert("出了点问题")
            b = false;
        }
    })
}

function toSkuProductList() {
    window.location.href = "index.do?ran=" + Math.random();
};